package com.demo.swagger.mapper;

import com.demo.swagger.entity.User;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface UserDao {

    /**
     * 根据用户名查询指定用户
     * 比较简洁的写法
     * @param userName
     * @Results 结果映射集
     * @return
     */
    @Select("select * from user where username=#{userName} limit 0,1")
    @Results({
            @Result(property = "userName", column = "username", javaType = String.class),
            @Result(property = "age", column = "age")
    })
    User selectByUserName(String userName);

    /**
     * 查询所有的用户
     * @return
     */
    @Select("select * from user")
    @Results({
            @Result(property = "userName", column = "username")
    })
    List<User> selects();

    /**
     * 根据用户名删除
     * @Param 做参数的映射
     * @param userName
     * @return
     */
    @Delete("delete from user where username = #{name}")
    int deleteByUserName(@Param("name") String userName);

    /**
     * 插入一条记录
     * @param user
     * @return
     */
    @Insert("insert into user values(#{userName}, #{password}, #{age})")
    int insertUser(User user);

    /**
     * 更新指定用户的密码
     * @param pwd
     * @param userName
     * @return
     */
    @Update("update user set password = #{pwd} where username = #{name}")
    int updateUserPassword( @Param("name") String userName, @Param("pwd") String pwd);

}
